package henry;

import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class StringUtil {

    public static final List<Character> CHARS = Stream.of(',', '，', '。', '.', '!', '?', ';', '！', '？', '；').collect(Collectors.toList());

    /**
     * 全角转半角
     *
     * @param input
     * @return
     */
    public static String SBCToDBC(String input) {
        if (input == null) {
            return null;
        }
        char[] cc = input.toCharArray();
        for (int i = 0; i < cc.length; i++) {
            if (cc[i] == 12288) {
                // 表示空格
                cc[i] = (char) 32;
                continue;
            }
            if (cc[i] > 65280 && cc[i] < 65375) {
                cc[i] = (char) (cc[i] - 65248);
            }
        }
        return new String(cc);
    }

    /**
     * @param input
     * @return
     */
    public static String repalceAsEnFlag(String input) {
        if (StringUtils.isBlank(input)) {
            return null;
        }
        input = SBCToDBC(input);
        input = input.replace("。", ".");
        input = input.replace("？", "?");
        input = input.replace("！", "!");
        input = input.replace("，", ",");
        input = input.replace("；", ";");
        return input;
    }

    /**
     * 拆分句子
     */
    public static List<String> splitWord(String word, Integer limit, List<String> list) {
        int length = word.length();

        if (StringUtils.isBlank(word) || limit <= 0 || length < limit) {
            list.add(word);
            return list;
        }
        String temp = word.substring(0, limit);
        String suffix = word.substring(limit);


        char[] chars = temp.toCharArray();
        for (int i = chars.length - 1; i > 0; i--) {
            char ch = chars[i];
            if (CHARS.contains(ch)) {
                // 包含标点符号
                list.add(temp.substring(0, i + 1));
                String next = temp.substring(i + 1) + suffix;
                temp = "";
                list = splitWord(next, limit, list);
                break;
            }
        }
        if (StringUtils.isNotBlank(temp)) {
            // 向后找
            char[] suffArray = suffix.toCharArray();
            for (int i = 0; i < suffArray.length; i++) {
                char ch = suffArray[i];
                if (CHARS.contains(ch)) {
                    list.add(temp + suffix.substring(0, i + 1));
                    suffix = suffix.substring(i + 1);
                    list = splitWord(suffix, limit, list);
                    break;
                }
            }
        }

        return list;
    }

    public static void main(String[] args) {
        String word = "发现其实到现在，我的这一系列spring博文，都没有好好讲过spring容器启动的过程（第一篇中也是直接给定位到了refresh方法）。正好上一篇讲的纯注解启动类AnnotationConfigApplicationContext，这里我们再回顾一下：";
        System.out.println(word);
        System.out.println("-------------------");
        List<String> list = StringUtil.splitWord(word, 50, new ArrayList<>());
        System.out.println("------------------");
        list.forEach(System.out::println);

    }


    public static String[] split(String input) {
        if (input == null) {
            return null;
        }
        String convertResult = SBCToDBC(input);
        return convertResult.replaceAll("\\s+", ",").split(",");
    }

    public static String replaceAsComma(String input) {
        String comma = ",";
        input = SBCToDBC(input);
        input = input.replace("%", comma);
        input = input.replace(";", comma);
        input = input.replace("、", comma);
        if (input.startsWith(comma)) {
            input = input.substring(1, input.length());
        }
        if (input.endsWith(comma)) {
            input = input.substring(0, input.length() - 1);
        }
        return input;
    }

}
